<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class LeaveController extends BaseController {



	public function _initialize() {
		parent :: _initialize();
		$this->_top_menu_list = array (
			'left' => array (
				array (
					'type' => 'home',
					'href' => U('Mob/Activity/activitymap'
				)
			),


		), 'center' => array (
			'title' => '知道'
		));

		if(is_login()){
            if(check_auth('Weibo/Index/doSend')){
                $this->_top_menu_list['right'][]=array('type'=>'edit','href'=>U('Mob/Weibo/addWeibo'));
            }else{
                $this->_top_menu_list['right'][]=array('type'=>'edit','info'=>'你没有权限发布微博！');
            }
        }else{
            $this->_top_menu_list['right'][]=array('type'=>'edit','info'=>'登录后才能操作！');
        }




		$this->UserInfo = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
		$this->setMobTitle('知道');
		$this->assign('top_menu_list', $this->_top_menu_list);

	}


    public function vacationapply()
    {
     $uid = get_uid();
     if($uid==0)
     {
    $this->redirect('Member/index');
     }
     if(IS_POST)
     {
     $describe = I('post.describe',null,'string');
     $reasonType = I('post.reasonType',1,'int');
     $startTime = I('post.startTime',null,'string');
     $endTime = I('post.endTime',null,'string');
     $studentId = I('post.studentId',null,'int');
     $classId = I('post.classId',null,'int');
     $schooId = I('post.schooId',null,'int');
     $contact = I('post.contact',null,'string');
     $leave = D('Leave');
     $startTime = strtotime($startTime);
     $endTime= strtotime($endTime);
     $is_apply = $leave->is_ExistLeave($uid,$schooId,$startTime);

   // $this->ajaxReturn(array('status'=>0,'msg'=>M()->getLastSql()));
     if($is_apply)
     {
     $data= array('status'=>0,'msg'=>'时间段内已提交请假申请，不能重复审请');
     }else
     {
    $res = $leave->addLeave($uid,$reasonType,$describe,$startTime,$endTime,$studentId,$classId,$contact,$schooId);

    if($res)
    {
   $leave->sendRemind($res,1);
    $data= array('status'=>1,'msg'=>'提交成功');
  //  $data= array('status'=>0,'msg'=>$contact);
    }else
    {
    $data= array('status'=>0,'msg'=>'操作失败');
   // $data= array('status'=>0,'msg'=>M()->getLastSql());
    }
     }
     $this->ajaxReturn($data);


     }else
     {

     $is_teacher = D('Teacher')->is_teacher($uid);


     if($is_teacher)
     {
      $this->redirect('leave/leaveapproval');

     }



    $studentInfo = D('Student')->queryStudent($uid);


    if(empty($studentInfo))
    {
    $this->redirect('Student/register',array('errmsg'=>'请注册后操作!'));

    }
    if($studentInfo['verificFlag']!=1)
    {
     $this->redirect('Student/register',array('errmsg'=>'请审核后再操作!'));

    }
    $orgaInfo = D('Orga')->get_OrgaFullInfo($studentInfo['ClassId']);

    $this->assign('orgaInfo',$orgaInfo);
    $this->assign('studentInfo',$studentInfo);

    $this->display();

     }

    }

    public function leaveapproval()
    {

     $teacherId = get_uid();
     if($teacherId==0)
     {
    $this->redirect('Member/index');

     }

    $teacher = D('Teacher')->query_Teacher($teacherId);
    $student = D('Student');
    if($teacher)
    {
    $leaveRecord = D('Leave')->authstrLeaveList($teacherId,$teacher['schooId']);

    $count =1;
    foreach($leaveRecord as &$v)
    {
    $v['sortId']= $count;
    $v['name']= $student->queryStudent($v['applyUid'])['name'];
    $count++;

    }
    unset($v);

   $this->assign('leaveRecord',$leaveRecord);
   $this->display();
    }

     }

   public function approvaloperate()
   {
   	if(IS_POST)
   	{
   	 $serialno = I('post.serialno');
   	 $teacherId = get_uid();
   	 $approvalFlg = I('post.approvalFlg');
   	 $leave = D('Leave');
   	 $leaveDetails= $leave->check_ApprovalAuth($serialno,$teacherId);

   	 if(!$leaveDetails)
   	 {
   	 $data= array('status'=>0,'msg'=>'你没有审批权限!');
   	 }
   	 elseif($leaveDetails['approvalFlg']!=0)
   	 {
   	  $data= array('status'=>0,'msg'=>'已审批过，不能重复操作!');

   	 }else
   	 {
   	 $comment = I('post.comment',null,'string');
     $res = $leave->approvalOperate($serialno,$leaveDetails['applyUid'],$teacherId,$approvalFlg,$comment);

     // $this->ajaxReturn(array('status'=>0,'msg'=>M()->getLastSql()));


     if($res)
     {
     $leave->sendRemind($serialno,2);
     $data= array('status'=>1,'msg'=>'审批成功!');
     }else
     {
    $data= array('status'=>0,'msg'=>'操作失败!');
    // $data= array('status'=>0,'msg'=>M()->getLastSql());
     }

   	 }
    $this->ajaxReturn($data);

   	}else
   	{
     $serialno = I('serialno');
    $teacherId = get_uid();
    $leave= D('Leave');
    $leaveDetails = $leave->get_LeaveDetails($serialno);
    $studentInfo = D('Student')->queryStudent($leaveDetails['applyUid']);
    $is_auth = $leave->check_ApprovalAuth($serialno,$teacherId);
    if($is_auth)
    {
     if($leaveDetails['approvalFlg']==0)
     {
     $is_approval = 1;

     }else
     {
     $is_approval = 0;
     }
    $classInfo = D('Orga')->get_OrgaFullInfo($leaveDetails['classId'],$leaveDetails['schooId']);
    $this->assign('is_approval',$is_approval);
    $this->assign('studentInfo',$studentInfo);
    $this->assign('classInfo',$classInfo);

    $this->assign('leaveDetails',$leaveDetails);

    }
    $this->display();

   	}


   }

   public function leavedetails()
   {
   	$serialno = I('get.serialno');
   	$leave = D('Leave');
   	$leaveDetails = $leave->get_LeaveDetails($serialno); //取请假详情


   	if($leaveDetails)
   	{
   //	$leaveDetails['approvalName']=D('Teacher')->query_Teacher($leaveDetails['approvalUid']);
   //	$leaveDetails['ClassInfo'] = D('Orga')->get_OrgaFullInfo($leaveDetails['classId']);
   $leaveDetails['studentOrga']= D('Orga')->get_OrgaFullInfo($leaveDetails['classId']);
  $student= D('Student')->queryStudent($leaveDetails['applyUid']);
  $leaveDetails['studentName']=$student['name'];
  $leaveDetails['sex']=$student['sex'];
   $leaveDetails['approvalName']=D('Teacher')->query_Teacher($leaveDetails['approvalUid'])['teacheName'];

   	}
   	$this->assign('leaveDetails',$leaveDetails);
   // print_r($leaveDetails);
    $this->display();

   }






}



